package com.BlogPark.controller;

import com.BlogPark.common.utis.IdUtils;
import com.BlogPark.controller.exception.Code;
import com.BlogPark.controller.result.Result;
import com.BlogPark.pojo.Article;
import com.BlogPark.pojo.DetailsUser;
import com.BlogPark.service.ManagerService.ManageUserService;
import com.BlogPark.service.ManagerService.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 业峰
 * @Date: 2022/8/11 - 08 - 11 - 15:00
 * @version: 1.0
 * 后台管理响应：包括：1.文章审核、2.用户管理、3.用户反馈.
 */
@RestController
@RequestMapping("/manage")
public class ManagerController {
    @Autowired
    private ManagerService managerService;

    @Autowired
    private ManageUserService manageUserService;

    /**
     * 1.文章审核:将传入的文章对象，模拟审核直接true后添加到数据库中
     *
     * @param article
     * @return
     */
    @PostMapping("/articleAudit")
    public Result articleAudit(@RequestBody Article article) throws Exception {
        //设置文章的添加时间
        long currentTime = System.currentTimeMillis();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        Date date = new Date(currentTime);
        article.setDate(date);

        //设置文章id值
        String articleId = IdUtils.generateArticleId(article.getAutherId());
        article.setId(articleId);

        String msg = "";
        Integer code = 0;
        boolean flag = true;
        //模拟审核，直接true
        if (flag) {
            System.out.println("审核通过");
            try {
                int row = managerService.articleAudit(article);
                if (row != 0) {//影响行数大于0
//            if (true){//影响行数大于0
                    System.out.println("数据插入成功");
                    System.out.println(article);
                    code = Code.SAVE_OK;
                    msg = "数据保存成功";
                } else {
                    System.out.println("数据插入失败");
                    code = Code.SAVE_ERR;
                    msg = "数据保存失败";
                }
            }catch (Exception e){
                msg = "数据保存失败";
                code = Code.SAVE_ERR;
                return new Result(code, null, msg);
            }
        }
        return new Result(code, null, msg);
    }

    /**
     * 查询所有用户信息
     *
     * @return
     */
    @GetMapping("/users")
    public Result userManageSearchAll() {
        Integer code;
        String msg;
        List<DetailsUser> users = manageUserService.findAllUser();
        if (users != null && !users.isEmpty()) {
            //查询有数据说明查询成功
            code = Code.GET_OK;
            msg = "查询成功";
        } else {
            code = Code.GET_ERR;
            msg = "查询失败";
        }
        return new Result(code, users, msg);
    }

    @DeleteMapping("/deleteById/{userId}")
    public Result userManageDelete(@PathVariable("userId") Integer userId) {
        Integer code;
        String msg;
        int row = manageUserService.deleteUserById(userId);
        if (row == 1) {
            code = Code.DELETE_OK;
            msg = "用户信息删除成功";
        } else {
            code = Code.DELETE_ERR;
            msg = "用户信息删除失败";
        }
        return new Result(code,null, msg);
    }

}
